package com.pangu.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import com.pangu.ui.adapter.IGroupPinnedAdapter;

/**
 * 头部固定分组ExpandableListView
 * 
 * @author cxlin
 * 
 * @date 2014-11-25 下午3:23:48
 */
public class PinnedExpandableListView extends MDataExpandableListView {

	private View pinnedView;
	private int pinnedViewWidth;
	private int pinnedViewHeight;

	public PinnedExpandableListView(Context context) {
		super(context);
	}

	public PinnedExpandableListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PinnedExpandableListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		if (pinnedView == null) {
			ExpandableListAdapter expandableListAdapter = getExpandableListAdapter();
			if (expandableListAdapter != null) {
				if (expandableListAdapter instanceof IGroupPinnedAdapter) {
					pinnedView = ((IGroupPinnedAdapter) expandableListAdapter).getPinnedHeadView();

				} else {
					throw new RuntimeException("adapter error");

				}
			}
		}

		if (pinnedView.getLayoutParams() == null) {
			pinnedView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
		}
		measureChild(pinnedView, widthMeasureSpec, heightMeasureSpec);
		pinnedViewWidth = pinnedView.getMeasuredWidth();
		pinnedViewHeight = pinnedView.getMeasuredHeight();
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		refreshPinnedHead();
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);

		ExpandableListAdapter expandableListAdapter = getExpandableListAdapter();
		if (pinnedView != null && expandableListAdapter != null && expandableListAdapter.getGroupCount() > 0) {
			drawChild(canvas, pinnedView, getDrawingTime());
		}
	}

	/**
	 * 刷新头部
	 */
	public void refreshPinnedHead() {
		if (pinnedView == null) {
			return;
		}
		int firstVisiblePos = getFirstVisiblePosition();
		int nextPos = firstVisiblePos + 1;
		int firstVisibleGroupPos = ExpandableListView
				.getPackedPositionGroup(getExpandableListPosition(firstVisiblePos));

		ExpandableListAdapter expandableListAdapter = getExpandableListAdapter();
		if (expandableListAdapter != null) {
			int nextGroupPos = ExpandableListView.getPackedPositionGroup(getExpandableListPosition(nextPos));
			int paddingLeft = getPaddingLeft();
			if (nextGroupPos == firstVisibleGroupPos + 1) {
				View view = getChildAt(1);
				if (view.getTop() < pinnedViewHeight) {
					int delta = pinnedViewHeight - view.getTop();
					pinnedView.layout(paddingLeft, -delta, paddingLeft + pinnedViewWidth, pinnedViewHeight - delta);

				} else {
					pinnedView.layout(paddingLeft, 0, paddingLeft + pinnedViewWidth, pinnedViewHeight);
				}

			} else {
				pinnedView.layout(paddingLeft, 0, paddingLeft + pinnedViewWidth, pinnedViewHeight);
			}

			if (expandableListAdapter instanceof IGroupPinnedAdapter) {
				((IGroupPinnedAdapter) expandableListAdapter).refreshPinnedView(pinnedView, firstVisibleGroupPos);
				invalidate(new Rect(0, 0, pinnedViewWidth, pinnedViewWidth));

			} else {
				throw new RuntimeException("adapter error");

			}
		}
	}
}
